//
//  CommentTableViewCell.m
//  CommentPage
//
//  Created by 欧杨 on 2020/11/27.
//

#import "CommentTableViewCell.h"
#import "CommentCellViewModel.h"
#import <Masonry/Masonry.h>

#define thumbImg @"outline_thumb_up_alt_black_24pt"
#define replyImg @"outline_textsms_black_24pt"
#define avatarImg @"an"

static void *PersonNameContext = &PersonNameContext;
static void *isLikeContext = &isLikeContext;

@interface CommentTableViewCell()

@property(strong, nonatomic) CommentCellViewModel *viewModel;
@property(strong, nonatomic) UIImageView *avatar;
@property(strong, nonatomic) UILabel *userNmae;
@property(strong, nonatomic) UILabel *date;
@property(strong, nonatomic) UILabel *content;
@property(strong, nonatomic) UIButton *checkReplies;
@property(strong, nonatomic) UIButton *like;
@property(strong, nonatomic) UILabel *likeNum;
@property(strong, nonatomic) UIButton *reply;
@property(strong, nonatomic) UILabel *replyNum;
//@property(strong, nonatomic) UIButton *delete;

@end

@implementation CommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIImage *likeImage = [[UIImage imageNamed:thumbImg] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImage *replyImage = [[UIImage imageNamed:replyImg] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.contentView.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.contentView.layer.borderWidth = 1;
    _avatar = [[UIImageView alloc] init];
    _userNmae = [[UILabel alloc] init];
    _date = [[UILabel alloc] init];
    _content = [[UILabel alloc] init];
    _checkReplies = [UIButton buttonWithType:UIButtonTypeSystem];
    _like = [[UIButton alloc] init];
    _likeNum = [[UILabel alloc] init];
    _reply = [[UIButton alloc] init];
    _replyNum = [[UILabel alloc] init];
    
    
    /*
     *set image
    */
    _avatar.layer.cornerRadius = 30;
    _avatar.layer.masksToBounds = YES;
    _avatar.clipsToBounds = YES;
    [_like setImage:likeImage forState:UIControlStateNormal];
    [_reply setImage:replyImage forState:UIControlStateNormal];
    
    /*
     *set label
    */
    [_content setFont:[_content.font fontWithSize:24]];
    _content.preferredMaxLayoutWidth = self.contentView.frame.size.width;
    _content.numberOfLines = 0;
    //_content.adjustsFontSizeToFitWidth = YES;
    [_userNmae setFont:[_content.font fontWithSize:26]];
    [_date setFont:[_content.font fontWithSize:20]];
    
    [self.contentView addSubview:_avatar];
    [self.contentView addSubview:_userNmae];
    [self.contentView addSubview:_date];
    [self.contentView addSubview:_content];
    //[self.contentView addSubview:_checkReplies];
    [self.contentView addSubview:_like];
    [self.contentView addSubview:_likeNum];
    [self.contentView addSubview:_reply];
    [self.contentView addSubview:_replyNum];
    
}

- (void)layoutSubviews
{
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(60, 60));
        make.left.top.equalTo(self.contentView).offset(10);
    }];
    [_userNmae mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatar);
        make.left.equalTo(_avatar.mas_right).offset(20);
    }];
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_avatar);
        make.left.equalTo(_avatar.mas_right).offset(20);
    }];
    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatar.mas_bottom).offset(20);
        make.left.equalTo(self.contentView).offset(30);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    /*
    [_checkReplies mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_content.mas_bottom).offset(15);
        make.left.equalTo(_avatar.mas_right).offset(15);
    }];
     */
    [_reply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(32, 32));
        make.top.equalTo(_content.mas_bottom).offset(10);
        make.right.equalTo(self.contentView).offset(-30);
    }];
    [_like mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(32, 32));
        make.top.equalTo(_content.mas_bottom).offset(10);
        make.right.equalTo(_reply.mas_left).offset(-40);
    }];
    [_likeNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_like);
        make.left.equalTo(_like.mas_right).offset(5);
    }];
    [_replyNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_reply);
        make.left.equalTo(_reply.mas_right).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
    }];
}

- (void)bindViewModel:(CommentCellViewModel *)viewModel
{
    _viewModel = viewModel;
    UIImage *avatarImage = [UIImage imageNamed:avatarImg];
    [_avatar setImage:avatarImage];
    _userNmae.text = _viewModel.userName;
    _content.text = _viewModel.content;
    _likeNum.text = [NSString stringWithFormat:@"%lu", _viewModel.likeNum];
    _replyNum.text = [NSString stringWithFormat:@"%lu", _viewModel.replyNum];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    _date.text = [dateFormatter stringFromDate:_viewModel.date];
    
    [viewModel addObserver:self forKeyPath:@"isLike" options:NSKeyValueObservingOptionNew context:isLikeContext];
    [viewModel addObserver:self forKeyPath:@"likeNum" options:NSKeyValueObservingOptionNew context:isLikeContext];
    if (_viewModel.isLike)
        _like.tintColor = UIColor.redColor;
    else
        _like.tintColor = UIColor.systemBlueColor;
    [_like addTarget:_viewModel
              action:@selector(likeClicked)
    forControlEvents:UIControlEventTouchUpInside];
    [self layoutSubviews];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark -BUTTON

- (void)likeClicked
{
    [_viewModel likeClicked];
}

#pragma mark -KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (context == isLikeContext) {
        if ([[change valueForKey:NSKeyValueChangeNewKey] boolValue]) {
            _like.tintColor = UIColor.redColor;
            _likeNum.text = [NSString stringWithFormat:@"%lu", _viewModel.likeNum];
        } else {
            _like.tintColor = UIColor.systemBlueColor;
            _likeNum.text = [NSString stringWithFormat:@"%lu", _viewModel.likeNum];
        }
    }
}

@end
